package com.hyc.myvideo.ui.fragment.live;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.acmenxd.toaster.Toaster;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.hyc.myvideo.R;
import com.hyc.myvideo.data.adapter.LiveViewAdapter;
import com.hyc.myvideo.data.api.model.ModelLive;
import com.hyc.myvideo.ui.activity.live.PlayerActivity;
import com.hyc.myvideo.ui.activity.my.LoginActivity;
import com.hyc.myvideo.ui.activity.my.RechargeActivity;
import com.hyc.myvideo.ui.base.BaseFragment;
import com.hyc.myvideo.ui.view.DialogLivePay;
import com.hyc.myvideo.ui.view.DialogLiveVIP;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 直播列表
 *
 * @author gyb
 * @version 1.0
 * @created 2017/07/21
 */
public class LiveViewFragment extends BaseFragment implements AdapterView.OnItemClickListener {
    @BindView(R.id.common_gridview_id)
    PullToRefreshGridView toRefreshGridView;
    //分类id
    private String id = "";
    //页数
    private int page = 1;
    //是否刷新
    private boolean isRefresh = true;
    private LiveViewAdapter adapter = null;
    private List<ModelLive> ListData = null;
    private DialogLiveVIP dVIP = null;
    private DialogLivePay dPay = null;
    public static LiveViewFragment newInstance(String sid) {
        LiveViewFragment viewFragment = new LiveViewFragment();
        viewFragment.id = sid;
        return viewFragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_common_gridview_10;
    }

    @Override
    protected void initView() {
        dVIP = new DialogLiveVIP(vIP);
        dPay = new DialogLivePay(pay);
        ListData = new ArrayList<>();
        toRefreshGridView.getRefreshableView().setNumColumns(2);
        adapter = new LiveViewAdapter(getActivity(), getDownloadList());
        toRefreshGridView.setAdapter(adapter);
        toRefreshGridView.setOnItemClickListener(this);
        toRefreshGridView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
                requestData(true);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
                requestData(false);
            }
        });
    }

    //测试数据
    private List<ModelLive> getDownloadList() {
        ListData.add(new ModelLive(0, "http://imgstore.cdn.sogou.com/app/a/100540002/809411.jpg", "100", "杨幂", "中国内地影视女演员、流行乐歌手、影视制片人"));
        ListData.add(new ModelLive(1, "http://e.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=75aaa91fa444ad342eea8f83e59220c2/0bd162d9f2d3572cf556972e8f13632763d0c388.jpg", "99", "杨幂01", "中国内地影视女演员、流行乐歌手、影视制片人"));
        ListData.add(new ModelLive(2, "http://imgstore.cdn.sogou.com/app/a/100540002/809411.jpg", "100", "杨幂", "中国内地影视女演员、流行乐歌手、影视制片人"));
        return ListData;
    }

    @Override
    protected void initData() {
        requestData(true);
    }

    @Override
    protected void onNexts(String r, String method) {

    }

    @Override
    protected void onError() {
        onRefresh();
    }

    private void requestData(boolean isRefresh) {
        this.isRefresh = isRefresh;
        if (isRefresh) {
            page = 1;
        } else {
            page++;
        }
        api.requestFollowData(page);
    }

    //加载完成后停止刷新
    private void onRefresh() {
        if (toRefreshGridView != null) {
            toRefreshGridView.onRefreshComplete();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (ListData != null && !ListData.isEmpty()) {
            final ModelLive modelLive = ListData.get(position);
            //0=免费，1=vip,2=收费
            switch (modelLive.getType()) {
                case 0:
                    startActivity(PlayerActivity.class, null);
                    break;
                case 1:
                    //判断是否登陆
                    LoginActivity.checkLogin(getActivity(), new LoginActivity.LoginCallback() {
                        @Override
                        public void onLogin(){
                            //判断用户身份 0=普通会员,1=vip会员,2=永久vip会员
                            switch (0) {
                                //提示开通VIP
                                case 0:
                                    toRefreshGridView.postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            dVIP.show(getFragmentManager(), "开通VIP");
                                        }
                                    }, 500);
                                    break;
                                default:
                                    startActivity(PlayerActivity.class, null);
                                    break;
                            }
                        }
                    });
                    break;
                case 2:
                    //判断是否登陆
                    LoginActivity.checkLogin(getActivity(), new LoginActivity.LoginCallback() {
                        @Override
                        public void onLogin(){
                            //判断用户身份 0=普通会员,1=vip会员,2=永久vip会员
                            switch (0) {
                                //提示开通VIP
                                case 2:
                                    startActivity(PlayerActivity.class, null);
                                    break;
                                default:
                                    toRefreshGridView.postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            dPay.setModelLive(modelLive);
                                            dPay.show(getFragmentManager(), "提示收费");
                                        }
                                    }, 500);
                                    break;
                            }
                        }
                    });
                    break;

            }
        }
}

    /***
     * 验证后直接跳转直播界面
     * @param cls
     * @param value
     */
    private void startActivity(Class<?> cls, String value) {
        startActivity(new Intent(getActivity(), cls).putExtra("url", value));
    }

    //提示开通VIP
    DialogLiveVIP.VIPInterface vIP = new DialogLiveVIP.VIPInterface() {
        @Override
        public void onLook() {
            Toaster.show("测试点击试看");
        }

        @Override
        public void onVIP() {
            startActivity(new Intent(appContext, RechargeActivity.class));
        }
    };

    //提示支付信息
    DialogLivePay.payInterface pay = new DialogLivePay.payInterface() {
        @Override
        public void onLook() {
            Toaster.show("测试点击试看");
        }

        @Override
        public void onPay() {
            Toaster.show("测试点击支付");
        }
    };

}
